package main;

import java.util.ArrayList;
import java.util.List;

/**
 * 最小时间差，，排序比较相邻，优化：1440长度数组表示一天的时间
 * n 1
 * 如果哈希表的键的数目是固定的，并且数目不大，
 * 那么也可以用数组来模拟哈希表，数组的下标对应哈希表的键，数组的值对应哈希表的值
 */
public class FindMinDifference {
    public static void main(String[] args) {
        List<String> timeArr = new ArrayList<>();
        timeArr.add("23:50");
        timeArr.add("23:59");
        timeArr.add("00:00");
        System.out.println(new FindMinDifference().findMinDifference(timeArr));
    }
    public int findMinDifference(List<String> timePoints){
        if (timePoints.size() > 1440){
            return 0;
        }
        boolean[] minuteFlags = new boolean[1440];
        for (String time : timePoints){
            String[] temps = time.split(":");
            int min = Integer.parseInt(temps[0])*60 + Integer.parseInt(temps[1]);
            if (minuteFlags[min]){
                return 0;
            }
            minuteFlags[min] = true;
        }
        return getTime(minuteFlags);
    }
    private int getTime(boolean[] minuteFlags){
        int minDiff = minuteFlags.length - 1;
        int prev = -1;
        int first = minuteFlags.length - 1;
        int last = -1;
        for (int i=0;i < minuteFlags.length;i++){
            if (prev > 0){
                minDiff = Math.min(i - prev,minDiff);
            }
            prev = i;
            first = Math.min(i,first);
            last = Math.max(i,last);
        }
        minDiff = Math.min(first + minuteFlags.length - last,minDiff);
        return minDiff;
    }
}
